

var pushReceive ={
        openPanel:function(content){
		var customer = JSON.parse(content);
           //toPanelID
	    if(customer && customer.imei){
           //alert("zhunbei tiaozhuan"+customer.imei);
            //跳转到toPanelID
            //	$.afui.loadContent("#"+customer.toPanelID);
	       var IMEI =customer.imei;
		   var device = localStorage.getItem(IMEI);
				 localStorage.setItem("device", device);
			if (localStorage.getItem("loginUser") != null){
           		 $.afui.loadContent("#notice", false, false, "none");
 				}else{
 				$.afui.loadContent("#login", false, false, "none");
 				}
            }
          /*  if(content){
                        $.afui.loadContent("#notice", false, false, "none");
                       }*/
        },
        alert:function(){
            popuo("ab");
        }
    }

  push_receive ={
   receiveNotification:function(content){
  console.log("content:"+content);
           var customer = JSON.parse(content);
            if(customer && customer.imei){
                         var IMEI=customer.imei;
                       //   alert(IMEI);
                 var device = localStorage.getItem(IMEI);
                // alert("device:"+device);
              localStorage.setItem("device", device);
               sendCmd({
                      c : "uGetNewMessage",
                      imei : IMEI
                  });
             }
         },
         alert:function(){
                      popuo("ab");
                  }
  }




// 验证码
var seconds = 300;
var timer;
function changeLink() {
	if (seconds > 1) {--seconds;
		$("#getCodeBtn").text("请等待(" + seconds + ")");
		$("#getCodeBtn2").text("请等待(" + seconds + ")");
		$("#getCodeBtn3").text("请等待(" + seconds + ")");
	} else
		initGetSmsCode();
}

function smsVerifyCallback(data) {
	if (data.success) {
		//Device.uBandTerminalCallback();
		stoper();
		seconds = 300;
		timer = setInterval(changeLink, 1000);
		popuo("验证码已发送到手机，请注意查收");
	} else {
		if (data.error == 1){
			//Device.uBandTerminalCallback();
			stoper();
			popuo("系统错误：验证码发送失败，请稍后重试");
		}
		else if (data.error == 2){
			//Device.uBandTerminalCallback();
			stoper();
			popuo("手机号码不存在");
		}

		initGetSmsCode();
	}
}

function initGetSmsCode() {
	$("#getCodeBtn").attr("href", "javascript: smsVerify(1);");
	$("#getCodeBtn").attr("class", "button blue");
	$("#getCodeBtn").text("获取验证码");
	$("#getCodeBtn2").attr("href", "javascript: smsVerify(2);");
	$("#getCodeBtn2").attr("class", "button blue");
	$("#getCodeBtn2").text("获取验证码");
	$("#getCodeBtn3").attr("href", "javascript: smsVerify(3);");
	$("#getCodeBtn3").attr("class", "button blue");
	$("#getCodeBtn3").text("获取验证码");
	seconds = 300;
	clearInterval(timer);
}

function smsVerify(type) {
	if (type == 1) {
		if (validateRegister()) {
			 if(isConnected){
						 	$("#getCodeBtn").attr("href", "javascript: void(0);");
             				$("#getCodeBtn").attr("class", "button gray");
                           	sendCmd({
                            				c : "smsVerify",
                            				type : type,
                            				mobile : $("#register-mobile").val().trim()
                            			});
                           	 Device.showMask_blockUI_2();
                           	// Device.uBandTerminalCallback_10();
							//overTimeCallback_time("getCodeBtn","smsVerify",315000,"1");
             }else{
                   popuo("连接超时，请检查网络设置");
                   socket.connect();
             }
		}
	} else if(type == 2){
		if ($("#forgot-mobile").val().trim() == "") {
			popuo("手机号码不能为空");
			return;
		}else if (!/^(?![,;])[*#+,;0-9]{2,20}$/.test($("#forgot-mobile").val().trim())) {
			popuo("手机号码格式不正确");
			return ;
        }
		 if(isConnected){
						$("#getCodeBtn2").attr("href", "javascript: void(0);");
         				$("#getCodeBtn2").attr("class", "button gray");
						sendCmd({
							c : "smsVerify",
							type : type,
							mobile : $("#forgot-mobile").val().trim()
						});
						 Device.showMask_blockUI_2();
						// Device.uBandTerminalCallback_10();
						//overTimeCallback_time("getCodeBtn2","smsVerify",315000,"2");
		 }else{
               popuo("连接超时，请检查网络设置");
               socket.connect();
          }
	}else{
		if ($("#bindMobile-mobile").val().trim() == "") {
			popuo("手机号码不能为空");
			return;
		}else if (!/^(?![,;])[*#+,;0-9]{2,20}$/.test($("#bindMobile-mobile").val().trim())) {
        		popuo("手机号码格式不正确");
        		return ;
        }
		 if(isConnected){
						$("#getCodeBtn3").attr("href", "javascript: void(0);");
         				$("#getCodeBtn3").attr("class", "button gray");
         				
						sendCmd({
							c : "smsVerify",
							type : 1,
							mobile : $("#bindMobile-mobile").val().trim()
						});

						 Device.showMask_blockUI_2();
						// Device.uBandTerminalCallback_10();
						//overTimeCallback_time("getCodeBtn3","smsVerify",315000,"3");
		 }else{
               popuo("连接超时，请检查网络设置");
               socket.connect();
          }
	}
}

function stopBubble(e) {
	//如果提供了事件对象，则这是一个非IE浏览器
	if ( e && e.stopPropagation )
	    //因此它支持W3C的stopPropagation()方法
	    e.stopPropagation();
	else
	    //否则，我们需要使用IE的方式来取消事件冒泡
	    window.event.cancelBubble = true;
	}


//阻止浏览器的默认行为
function stopDefault(e) {
    //阻止默认浏览器动作(W3C)
    if ( e && e.preventDefault )
        e.preventDefault();
    //IE中阻止函数器默认动作的方式
    else
        window.event.returnValue = false;
    return false;
}



// 登录
function loginCallback(data) {
	console.log("路过开始");
	if (data.success) {
		if(data.type == 1){
		//Device.uBandTerminalCallback();
		stoper();
		localStorage.setItem("loginUser", data.user);
			$.afui.loadContent("#bindMobile", false, false, "none");
		}else{
			//Device.uBandTerminalCallback();
			stoper();
			localStorage.setItem("loginUser", data.user);
		sendCmd({
			c : 'uApkPulse',
			id : localStorage.getItem("loginUser").split(",")[0],
			lastLogin :localStorage.getItem("loginUser").split(",")[4]
		});

		var id=localStorage.getItem("loginUser").split(",")[0];
	
		// 跨view跳转bug修复
		$("#loginView").find(".panel").removeClass('active');
		// 进入我的终端界面
		$.afui.loadContent("#main", false, false, "none");
		// $("#main").trigger('panelbeforeload');
		$("#main").trigger('panelload');
		initNav();
		}
	} else {
		//Device.uBandTerminalCallback();
		stoper();
		popuo("用户名/密码不正确");
	}
	console.log("路过1");
	$("#loginBtn").attr("href", "javascript: login();");
	console.log("路过2");
	$("#loginBtn").attr("class", "button block blue");
	console.log("路过3");
}

function login() {
	if ($("#login-mobile").val().trim() == "")
		popuo("手机号不能为空");
	else if ($("#login-password").val().trim() == "")
		popuo("密码不能为空");
	else {

		//popuo("命令未已发出！");
 		if(isConnected){
 					$("#loginBtn").attr("href", "javascript: void(0);");
        			$("#loginBtn").attr("class", "button block gray");
               	 sendCmd({
                 			c : "login",
                 			mobile : $("#login-mobile").val().trim(),
                 			password : $("#login-password").val()
                 		});
               	 Device.showMask_blockUI_1();
               	// Device.uBandTerminalCallback_10();
               	overTimeCallback_time("loginBtn","login",10000);
        }else{
               	popuo("连接超时，请检查网络设置");
               	socket.connect();
         }
	}
}

// 注销
function logout() {
	 if(isConnected){
               	sendCmd({
                		c : "logout",
                		id : localStorage.getItem("loginUser").split(",")[0]
                	});
                window.plugins.jPushPlugin.setAlias("");
				localStorage.removeItem("loginUser");
				localStorage.removeItem("devices");
				$("#deviceList").empty();
				 localStorage.clear();
				// 跨view跳转bug修复
				$("#setting").removeClass('active');
				// 跳转登录页
				$.afui.loadContent("#login", false, false, "none");
     	}else{
          	window.plugins.jPushPlugin.setAlias("");
          		localStorage.removeItem("loginUser");
          		localStorage.removeItem("devices");
          		 $("#deviceList").empty();
          		localStorage.clear();
          		// 跨view跳转bug修复
          		$("#setting").removeClass('active');
          		// 跳转登录页
          		$.afui.loadContent("#login", false, false, "none");
     	}
}

function logoutCallback(data) {
	if (data.success) {
		if (data.force)
			popuo("已在其他客户端登录，本次登录已失效，如有疑问，请咨询系统管理员");
		window.plugins.jPushPlugin.setAlias("");
		localStorage.removeItem("loginUser");
		localStorage.removeItem("devices");
		 $("#deviceList").empty();
		 localStorage.clear();
		// 跨view跳转bug修复
		$("#setting").removeClass('active');
		$("#deviceView").find(".panel").removeClass('active');
		$("#main").removeClass('active');
		// 跳转登录页
		$.afui.loadContent("#login", false, false, "none");
	}
}

// 注册
function validateRegister() {
	if ($("#register-mobile").val().trim() == "") {
		popuo("手机号码不能为空");
		return false;
	} else if (!/^(?![,;])[*#+,;0-9]{2,20}$/.test($("#register-mobile").val().trim())) {
		popuo("手机号码格式不正确");
		return false;
	} else if ($("#register-name").val().trim() == "") {
		popuo("昵称不能为空");
		return false;
	} else if ($("#register-password").val().trim() == "") {
		popuo("密码不能为空");
		return false;
	} else if ($("#register-password").val().trim().length < 6) {
		popuo("密码长度不能小于6位");
		return false;
	} else if ($("#register-password").val().trim() != $("#register-repassword").val().trim()) {
		popuo("两次输入的密码不一致");
		return false;
	}
	return true;
}

function registerCallback(data) {
	if (data.success) {
		//Device.uBandTerminalCallback();
		stoper();
		initGetSmsCode();
		localStorage.setItem("loginUser", data.user);
		// 跨view跳转bug修复
		$("#loginView").find(".panel").removeClass('active');
		$("#main").trigger('panelload');
		//uGetNewMessage();
		// 进入我的终端界面
		$.afui.loadContent("#main", false, false, "none");
		initNav();
		
	} else {
		if (data.error == 1) {
		//Device.uBandTerminalCallback();
			stoper();
			popuo("手机号码已存在");
			initGetSmsCode();
		} else if (data.error == 2){
		 //Device.uBandTerminalCallback();
		 stoper();
		 popuo("验证码不正确");
		}

	}
	$("#registerBtn").attr("href", "javascript: register();");
	$("#registerBtn").attr("class", "button block blue");
}

function register() {
	if (validateRegister()) {
		if ($("#register-code").val().trim() == "") {
			popuo("验证码不能为空");
			//Device.uBandTerminalCallback();
			stoper();
		} else {
		if(isConnected){
					$("#registerBtn").attr("href", "javascript: void(0);");
        			$("#registerBtn").attr("class", "button block gray");
                   	sendCmd({
                    				c : "register",
                    				smsVerify : $("#register-code").val().trim(),
                    				mobile : $("#register-mobile").val().trim(),
                    				password : $("#register-password").val(),
                    				name : $("#register-name").val(),
                    				sex : $("#register-sex").val()
                    			});
                   	 Device.showMask_blockUI_2();
                   //	 Device.uBandTerminalCallback_10();
                   overTimeCallback_time("registerBtn","register",10000);
   		}else{
              popuo("连接超时，请检查网络设置");
              socket.connect();
   		}
		}
	}
}

// 找回密码
function forgotCallback(data) {
	if (data.success) {
		//Device.uBandTerminalCallback();
		stoper();
		initGetSmsCode();
		$.afui.loadContent("#reset", false, false, "none");
		$("#reset-token").val(data.token);
	} else {
		//Device.uBandTerminalCallback();
		stoper();
		popuo("验证码不正确");
	}
	$("#forgotBtn").attr("href", "javascript: forgot();");
	$("#forgotBtn").attr("class", "button block blue");
}

function forgot() {
	if ($("#forgot-mobile").val().trim() == ""){
		popuo("手机号码不能为空");
		//Device.uBandTerminalCallback();
		stoper();
	}
	else if ($("#forgot-code").val().trim() == ""){
		popuo("验证码不能为空");
		//Device.uBandTerminalCallback();
		stoper();
	}
	else {
		 if(isConnected){
		 				$("#forgotBtn").attr("href", "javascript: void(0);");
         				$("#forgotBtn").attr("class", "button block gray");
                       	sendCmd({
                        			c : "uGetPassword",
                        			mobile : $("#forgot-mobile").val().trim(),
                        			smsVerify : $("#forgot-code").val().trim()
                        		});
                       	 Device.showMask_blockUI_2();
                       	 //Device.uBandTerminalCallback_10();
                       	 overTimeCallback_time("forgotBtn","forgot",10000);
         }else{
               popuo("连接超时，请检查网络设置");
               socket.connect();
          }
	}
}

// 重置密码
function resetCallback(data) {
	if (data.success) {
		//Device.uBandTerminalCallback();
		stoper();
		popuo("密码已重置，请使用新密码登录系统");
		$.afui.loadContent("#login", false, false, "none");
	} else {
		//Device.uBandTerminalCallback();
		stoper();
		popuo("非法请求，请重试");
		$.afui.loadContent("#forgot", false, false, "none");
	}
	$("#resetBtn").attr("href", "javascript: reset();");
	$("#resetBtn").attr("class", "button block blue");
}

function reset() {
	if ($("#reset-password").val().trim() == "")
		popuo("密码不能为空");
	else if ($("#reset-password").val().trim().length < 6)
		popuo("密码长度不能小于6位");
	else if ($("#reset-password").val().trim() != $("#reset-repassword").val().trim())
		popuo("两次输入的密码不一致");
	else {
		if(isConnected){
				$("#resetBtn").attr("href", "javascript: void(0);");
        		$("#resetBtn").attr("class", "button block gray");
                   	sendCmd({
                    			c : "uResetPassword",
                    			token : $("#reset-token").val().trim(),
                    			password : $("#reset-password").val()
                    		});
                   	 Device.showMask_blockUI_2();
                   	// Device.uBandTerminalCallback_10();
                   	overTimeCallback_time("resetBtn","reset",10000);
        }else{
              popuo("连接超时，请检查网络设置");
              socket.connect();
        }

	}
}
function bindMobile() {
	if ($("#bindMobile-mobile").val().trim() == ""){
		popuo("手机号码不能为空");
		//Device.uBandTerminalCallback();
		stoper();
	}
	else if (!/^(?![,;])[*#+,;0-9]{2,20}$/.test($("#bindMobile-mobile").val().trim())) {
     		popuo("手机号码格式不正确");
     		//Device.uBandTerminalCallback();
     		stoper();
     	}
	else if ($("#bindMobile-code").val().trim() == ""){
		popuo("验证码不能为空");
		//Device.uBandTerminalCallback();
		stoper();
	}
	else {
		 if(isConnected){
		 				$("#bindMobileBtn").attr("href", "javascript: void(0);");
         				$("#bindMobileBtn").attr("class", "button block gray");
                       	sendCmd({
                        			c : "saveMobile",
                        			id : localStorage.getItem("loginUser").split(",")[0],
                        			mobile : $("#bindMobile-mobile").val().trim(),
                        			smsVerify : $("#bindMobile-code").val().trim()
                        		});
                       	 Device.showMask_blockUI_2();
                       //	 Device.uBandTerminalCallback_10();
                       overTimeCallback_time("bindMobileBtn","bindMobile",10000);
         }else{
               popuo("连接超时，请检查网络设置");
               socket.connect();
          }
	}
}


function saveMobileCallback(data) {
	if (data.success) {
		//Device.uBandTerminalCallback();
		stoper();
		initGetSmsCode();
		localStorage.setItem("loginUser", data.user);
		sendCmd({
			c : 'uApkPulse',
			id : localStorage.getItem("loginUser").split(",")[0],
			lastLogin :localStorage.getItem("loginUser").split(",")[4]
		});
		var id=localStorage.getItem("loginUser").split(",")[0];
		// 跨view跳转bug修复
		$("#loginView").find(".panel").removeClass('active');
		// 进入我的终端界面
		$.afui.loadContent("#main", false, false, "none");
		// $("#main").trigger('panelbeforeload');
		$("#main").trigger('panelload');
		initNav();
	} else {
		if (data.error == 1) {
		//Device.uBandTerminalCallback();
		stoper();
			popuo("手机号码已存在");
			initGetSmsCode();
		} else if (data.error == 2){
		 //Device.uBandTerminalCallback();
		 stoper();
		 popuo("验证码不正确");
		}else{
			 //Device.uBandTerminalCallback();
			 stoper();
			 popuo("该手机号已被其它用户做账户使用");
		}

	}
	$("#bindMobileBtn").attr("href", "javascript:bindMobile();");
	$("#bindMobileBtn").attr("class", "button block blue");
}